package cdu.bbs.bbsv2.utils;
import java.sql.*;
import java.util.ResourceBundle;

public class JDBCUtil {
    private  Connection con = null;
    private  PreparedStatement ps = null;
    private static String driver;
    private static String url;
    private static String userName;
    private static String password;

    static{
        try {
            ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
            driver = bundle.getString("mysql.driver");
            url = bundle.getString("mysql.url");
            userName = bundle.getString("mysql.user");
            password = bundle.getString("mysql.password");
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public  Connection createCon(){
        try {
            con = DriverManager.getConnection(url,userName,password);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("Connection对象创建失败。。。。。");
        }
        return con;
    }

    public  PreparedStatement createStatement(String sql){
        try {
            ps = con.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ps;
    }

    public void close(){
        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(con !=null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public void close(ResultSet rs){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        close();
    }

}